//
//  GQHMessagesController.m
//  Xanthium
//
//  Created by GuanQinghao on 07/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHMessagesView.h"

#pragma mark Controller
#import "GQHMessagesController.h"
#import "GQHMessageController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHMessagesController () <GQHMessagesViewDelegate> {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHMessagesView *rootView;
    
    NSMutableArray * countArray;
}

@end


@implementation GQHMessagesController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHMessagesView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
    
    countArray = [NSMutableArray arrayWithObjects:@"0", @"0", @"0", @"0", nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"消息中心";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self getMessageCount:@""];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark ---- 获取消息数量
- (void)getMessageCount:(NSString *)count
{
    NSString * url = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"buMessageReceiveUser/1/unReadNum"];
    [GQHNetHelper qh_requestPOST:url parameters:@{@"userId":USER_GET_ID, @"newsCategory":@"01"} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        [countArray replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%@", data]];
        rootView.countArray = countArray;
    } failure:^(NSError *error) {
        
    }];
    
    NSString * url2 = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"buMessageReceiveUser/1/unReadNum"];
    [GQHNetHelper qh_requestPOST:url2 parameters:@{@"userId":USER_GET_ID, @"newsCategory":@"02"} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        [countArray replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%@", data]];
        rootView.countArray = countArray;
    } failure:^(NSError *error) {
        
    }];
    
    NSString * url3 = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"buMessageReceiveUser/1/unReadNum"];
    [GQHNetHelper qh_requestPOST:url3 parameters:@{@"userId":USER_GET_ID, @"newsCategory":@"03"} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        [countArray replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%@", data]];
        rootView.countArray = countArray;
    } failure:^(NSError *error) {
        
    }];
    
    NSString * url4 = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"buMessageReceiveUser/1/unReadNum"];
    [GQHNetHelper qh_requestPOST:url4 parameters:@{@"userId":USER_GET_ID, @"newsCategory":@"04"} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        [countArray replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%@", data]];
        rootView.countArray = countArray;
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    NSString *mesageType;
    
    if (0 == indexPath.row) {
        
        mesageType = @"01";
    } else if (1 == indexPath.row) {
        
        mesageType = @"02";
    } else if (2 == indexPath.row) {
        
        mesageType = @"04";
    } else {
        
        mesageType = @"03";
    }
    
    GQHMessageController *messageController = [[GQHMessageController alloc] init];
    messageController.qh_messageType = mesageType;
    messageController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messageController animated:YES];
}
#pragma mark --TargetMethod

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
